import javax.swing.JTextArea;


public class RandomPlayer extends Player {

	
	
	public RandomPlayer(int size, String name, JTextArea textArea) {
		super(size, name, textArea);
	}


	/*
	 * (non-Javadoc)
	 * @see Player#strike()
	 */
	@Override
	public int strike() {
		int x = 1;
		int y = 1;
		while (true) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			x = r.nextInt(gameSize) + 1;
			y = r.nextInt(gameSize) + 1;
			if (opBoard.getField(x, y) == Board.STROKE || opBoard.getField(x, y) == Board.TRIED || opBoard.getField(x, y) == Board.SUNK) {
				//tried - once again
			} else {
				//haven't tried
				int result = opponent.ask(x, y);
				if (result == Player.HIT) {
					//successful stroke
					opBoard.setField(x, y, Board.STROKE);
					allShipsFields--;
					if (allShipsFields > 0) {
						//still are opponents ships
						return Player.HIT;
					} else {
						//won
						return Player.WON;
					}
				} else if (result == Player.SUNK) {
					//successful stroke - sunk
					opBoard.setField(x, y, Board.STROKE);
					setOpSunk(x, y);
					allShipsFields--;
					if (allShipsFields > 0) {
						//still are opponents ships
						return Player.HIT;
					} else {
						//won
						return Player.WON;
					}
				} else {
					//unsuccessful stroke
					opBoard.setField(x, y, Board.TRIED);
					return Player.EMPTY;					
				}
			}
		}
	}
	
	

	
}
